package com.yanqu.road.server.cmd.unioncampwar;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.pb.unionCampWar.UnionCampWar;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.unioncampwar.UnionCampWarModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.unioncampwar.UnionCampWarPatronsMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.concurrent.CompletableFuture;


@Cmd(code = GameProtocol.S_UNION_CAMP_WAR_WORSHIP, desc = "点赞")
public class UnionCampWarWorshipCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (null == player) {
            return;
        }

        ret = player.getModule(UnionCampWarModule.class).checkWorship();
        if (ret == 0) {
            UnionCampWar.UnionCampWarWorshipRespMsg.Builder resp = UnionCampWar.UnionCampWarWorshipRespMsg.newBuilder();
            CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToCrossServer(player.getUserId(), CrossProtocol.C3_UNION_CAMP_WAR_WORSHIP, CommonProto.NullValueMsg.newBuilder(), resp);
            future.thenAccept(syncResponse -> {
                if (syncResponse.getErrorCode() != 0) {
                    UnionCampWarPatronsMgr.getLogger().error("玩家{},商帮点赞返回错误{}", player.getUserId(), syncResponse.getErrorCode());
                    return;
                }
                if (resp.getRet() == 0) {
                    player.getModule(UnionCampWarModule.class).worshipBack();
                }
                player.sendPacket(ClientProtocol.U_UNION_CAMP_WAR_WORSHIP, resp);
            });
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (null == player) {
            return;
        }

        if (ret != 0) {
            player.sendPacket(ClientProtocol.U_UNION_CAMP_WAR_WORSHIP, UnionCampWar.UnionCampWarWorshipRespMsg.newBuilder().setRet(ret));
        }

    }
}
